<template>
  <div class="user-modal">
    <el-dialog v-model="dialogVisible" title="新建用户" width="500" center>
      <div class="form">
        <el-form v-model="formData" label-width="80px" size="large">
          <el-form-item prop="name" label="用户名"
            ><el-input v-model="formData.name" placeholder="请输入用户名"
          /></el-form-item>
          <el-form-item prop="realname" label="真实姓名"
            ><el-input v-model="formData.realname" placeholder="请输入用户名"
          /></el-form-item>
          <el-form-item v-if="isNewRef" prop="password" label="密码"
            ><el-input
              v-model="formData.password"
              placeholder="请输入密码"
              show-password
          /></el-form-item>
          <el-form-item prop="cellphone" label="手机号码"
            ><el-input v-model="formData.cellphone" placeholder="请输入手机号"
          /></el-form-item>

          <el-form-item prop="roleId" label="选择角色">
            <el-select v-model="formData.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in entireRoles"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="departmentId" label="选择部门">
            <el-select v-model="formData.departmentId">
              <el-option
                v-for="item in entireDepartments"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option> </el-select
          ></el-form-item>
        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleComfirm"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useMainStore } from '@/store/main/main'
import { useSystemStore } from '@/store/main/system/system'
import { storeToRefs } from 'pinia'
import { reactive, ref } from 'vue'
// 新增用户数据
const formData = reactive<any>({
  name: '',
  realname: '',
  password: '',
  roleId: '',
  departmentId: '',
  cellphone: ''
})
const userId = ref()
const isNewRef = ref()
// 是否显示dialog
const dialogVisible = ref(false)
function showDialog(isNew: boolean = true, userData: any) {
  dialogVisible.value = true
  isNewRef.value = isNew
  userId.value = userData?.id
  if (!isNew && userData) {
    for (const key in formData) {
      formData[key] = userData[key]
    }
  } else {
    for (const key in formData) {
      formData[key] = ''
    }
  }
}
defineExpose({ showDialog })

// 获取roles和department
const mainStore = useMainStore()
const { entireRoles, entireDepartments } = storeToRefs(mainStore)
console.log(entireRoles)

// 新增用户
function handleComfirm() {
  // 隐藏dialog
  dialogVisible.value = false
  const systemStore = useSystemStore()
  if (isNewRef.value) {
    //创建新用户
    systemStore.postUsersAddAction(formData)
  } else {
    systemStore.patchUsersEditAction(formData, userId.value)
  }
}
</script>

<style lang="less" scoped>
.user-modal {
}
.form {
  padding: 0 40px;
}
</style>
